package com.newzqxq.koalamoney.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.entity.AutoPlanBean;
import com.newzqxq.koalamoney.widget.ColorTextView;
import com.newzqxq.koalamoney.widget.progress.RoundProgress;

import java.util.List;

/**
 * 定投adapter
 * Created by ZQXQ-Developer on 2017/7/19.
 */

public class AutoPlanAdapter extends BaseRecyclerAdapter {
    private List<AutoPlanBean> items;
    private Context mContext;
    public OnItemClickListener mItemClickListener;
    private LayoutInflater inflater;

    public AutoPlanAdapter(List<AutoPlanBean> items, Context mContext) {
        this.items = items;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(viewType, parent, false);
        return new mViewHolder(view, mItemClickListener);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.item_autoplan;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((mViewHolder) holder).bind(items.get(position));
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view);
    }

    public AutoPlanAdapter setmItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
        return this;
    }

    private class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView tvAutoData;//期数
        private TextView tvRate;//收益
        private TextView tvAutoIncome;//定投天数/金额
        private TextView tvAutoPerson;//人数
        private RoundProgress pAutoProgress;//进度
        private TextView tvProgress;
        private OnItemClickListener clickListener;
        private LinearLayout layout;

        public mViewHolder(View view, OnItemClickListener mItemClickListener) {
            super(view);
            tvAutoData = (TextView) view.findViewById(R.id.tvAutoData);
            tvRate = (TextView) view.findViewById(R.id.tvRate);
            tvAutoIncome = (TextView) view.findViewById(R.id.tvAutoIncome);
            tvAutoPerson = (TextView) view.findViewById(R.id.tvAutoPerson);
            pAutoProgress = (RoundProgress) view.findViewById(R.id.pAutoProgress);
            tvProgress = (TextView) view.findViewById(R.id.tvProgress);
            this.clickListener = mItemClickListener;
            layout = (LinearLayout) view.findViewById(R.id.llParent);
            layout.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.onItemClickListener(view);
            }
        }

        public void bind(AutoPlanBean plan) {
            tvAutoData.setText(plan.getDay());
            tvRate.setText(plan.getIncome());
            tvAutoIncome.setText(plan.getMoney());
            String pattern = "已有{" + plan.getPerson() + "}人出借";
            CharSequence chars = ColorTextView.from(pattern).withSeparator("{}")
                    .innerColor(0xFFE6454A).outerColor(0xFF666666).format();
            tvAutoPerson.setText(chars);
            pAutoProgress.setProgress(plan.getProgress());
            if (plan.getProgress() > 100) {
                tvProgress.setText(100 + "%");
            } else {
                tvProgress.setText(plan.getProgress() + "%");
            }
        }
    }
}
